package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.quality.domain.req.BcStationHqDetailReq;
import com.ruibang.glass.quality.entity.BcStationHqDetail;
import com.ruibang.glass.quality.entity.GlassBindTest;
import com.ruibang.glass.quality.mapper.GlassBindTestMapper;
import com.ruibang.glass.quality.service.BcStationHqDetailService;
import com.ruibang.glass.quality.service.GlassBindTestService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2023-11-15  09:17
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class GlassBindTestServiceImpl extends ServiceImpl<GlassBindTestMapper, GlassBindTest> implements GlassBindTestService {

    @Autowired
    private BcStationHqDetailService bcStationHqDetailService;

    @Override
    public Collection<BcStationHqDetail> getTransects(BcStationHqDetailReq bcStationHqDetailReq){
        /*LocalDateTime now = LocalDateTime.now();
        String startDate = now.minusDays(1).format(DateTimeFormatter.ISO_LOCAL_DATE);
        String endDate = now.format(DateTimeFormatter.ISO_LOCAL_DATE);
        BcStationHqDetailReq bcStationHqDetailRe = new BcStationHqDetailReq();
        bcStationHqDetailRe.setStartDate(startDate);
        bcStationHqDetailRe.setEndDate(endDate);*/
        List<BcStationHqDetail> bcs = bcStationHqDetailService.queryDataByTime(bcStationHqDetailReq);
        if(CollectionUtils.isNotEmpty(bcs)){
           for (BcStationHqDetail bc : bcs) {
               bc.setParentGlassId(bc.getId());
           }
           Map<String,BcStationHqDetail> bcStationHqDetailMap = bcs.stream().collect(Collectors.toMap(BcStationHqDetail::getId, b1->b1,(b2, b3)->b2));
           queryChildData(bcs,bcStationHqDetailMap);

            return bcStationHqDetailMap.values();
        }

        return new ArrayList<>();
    }

    private void queryChildData(List<BcStationHqDetail> bcStationHqDetails,Map<String,BcStationHqDetail> bcStationHqDetailMap){
        Map<String,BcStationHqDetail> newMap = bcStationHqDetails.stream().collect(Collectors.toMap(BcStationHqDetail::getParentGlassId, b1->b1,(b2, b3)->b2));
        List<GlassBindTest> glassBindTests = baseMapper.queryDataByOldIds(bcStationHqDetails.stream().map(BcStationHqDetail::getParentGlassId).collect(Collectors.toList()));
        if(CollectionUtils.isNotEmpty(glassBindTests)){
            List<BcStationHqDetail> newList = new ArrayList<>();
            for (GlassBindTest glassBindTest : glassBindTests) {
                if(glassBindTest.getNewID().substring(0,4).equals("SL01")){
                    BcStationHqDetail detail = newMap.get(glassBindTest.getOldID());
                    BcStationHqDetail rootDetail = bcStationHqDetailMap.get(detail.getId());
                    List<String> list = rootDetail.getGlassIds();
                    if(CollectionUtils.isEmpty(list)){
                        list = new ArrayList<>();
                    }
                    list.add(glassBindTest.getNewID());
                    rootDetail.setGlassIds(list);
                    detail.setParentGlassId(glassBindTest.getNewID());
                    bcStationHqDetailMap.put(detail.getId(),rootDetail);
                    newList.add(detail);
                }
            }

            if(CollectionUtils.isNotEmpty(newList)){
                queryChildData(newList,bcStationHqDetailMap);
            }

        }
    }
}
